/**
 * 
 */
package sopia.sg;

import sopia.cmm.CommonFunct;
import sopia.conn.DBAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * @author tatsu@gmail.com
 *
 */
public class G003Activity extends Activity {
	private DBAdapter db ;
	private Button addAccButton ;
	private EditText nameAccountEdit ;
	private EditText amountAccountEdit;
	private String screenID ;
	/**
	 * 
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.g003_layout);
		db = new DBAdapter(this);
		
		//init spinner
		Spinner unitSpinner = (Spinner) findViewById(R.id.unit_spin);
		ArrayAdapter adapter = ArrayAdapter.createFromResource(
		            this, R.array.unit, android.R.layout.simple_spinner_item);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    unitSpinner.setAdapter(adapter);
	    // screen ID call
		Intent receiveIntent = this.getIntent();
		screenID = receiveIntent.getStringExtra("ID");
		
	    ///back screen
	    
		final ImageButton backImagebutton = (ImageButton) findViewById(R.id.back_imagebutton);
		backImagebutton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				backToPreviousScreen(screenID);
			}
		});
		Toast.makeText(this,"" , Toast.LENGTH_LONG);
		// add account addAccName_button 
		addAccButton = (Button) findViewById(R.id.addAccName_button);
		nameAccountEdit = (EditText) findViewById(R.id.nameAcc_edit);
		amountAccountEdit = (EditText) findViewById(R.id.amountAcc_edit);
		
		addAccButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				insertAccount(nameAccountEdit.getText().toString(),amountAccountEdit.getText().toString());
			}
		});		
		
//		amountAccountEdit.
		
	}
	private void insertAccount(String nameAccount,String amountAccount){
		db.open();				
		if(CommonFunct.isNullOrEmpty(nameAccount)){
			nameAccount = null;
			Toast.makeText(this,"Please input [Account name]!" , Toast.LENGTH_LONG).show();
			nameAccountEdit.setFocusable(true);
			return;
		}
		if(CommonFunct.isNullOrEmpty(amountAccount)){
			nameAccount = "0";
		}
		Long id = db.insertAccount(nameAccount, amountAccount);
		//the row ID of the newly inserted row, or -1 if an error occurred 
		String contentMessage = "Account["+nameAccount;
		if(id < 0){			
			contentMessage += "]really exist your database.";
			Toast.makeText(this,contentMessage , Toast.LENGTH_LONG).show();
			nameAccountEdit.selectAll();
		}else{
			contentMessage += "]was added.";
			Toast.makeText(this, contentMessage , Toast.LENGTH_LONG).show();
			backToPreviousScreen(screenID);
		}
		db.close();
	}
	private void backToPreviousScreen(String screenID){
		Intent i = null ;
		if("G002".equals(screenID)) {
			i = new Intent(G003Activity.this, G002Activity.class);
		} else if("G005".equals(screenID)) {
			i = new Intent(G003Activity.this, G005Activity.class);
		}
		i.putExtra("ID", "G003");
//		Bundle sendBundle = new Bundle();
//		sendBundle.putLong("value", receiveValue);
//		i.putExtras(sendBundle);
		startActivity(i);
	}
}
